<?php

/**
 * This is the model class for table "advertisement".
 *
 * The followings are the available columns in table 'advertisement':
 * @property integer $advt_id
 * @property integer $user_id
 * @property string $text
 * @property string $publish_date
 * @property integer $comment_flow_id
 * @property integer $img_id
 * @property string $title
 */
class Advertisement extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Advertisement the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'advertisement';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, text', 'required'),
                        array('title, text', 'required'),
			array('user_id', 'length', 'max'=>10),
			array('publish_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('advt_id, user_id, text, publish_date, img_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
                return array(
                    'user' => array(self::BELONGS_TO, 'User', 'user_id'),
                    'advertisementcomments' => array(self::HAS_MANY, 'Advertisementscomment', 'advt_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'advt_id' => 'Объявление №',
			'user_id' => 'Пользователь',
			'text' => 'Текст',
			'publish_date' => 'Дата публикации',
			'comment_flow_id' => 'Comment Flow',
			'img_id' => 'Логотип',
			'title' => 'Заголовок',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('advt_id',$this->advt_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('publish_date',$this->publish_date,true);
		$criteria->compare('comment_flow_id',$this->comment_flow_id);
		$criteria->compare('img_id',$this->img_id);
		$criteria->compare('title',$this->title,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

        public function scopes()
        {
            return array(
                'latest'=>array(
                    'order' =>  'publish_date DESC',
                    'limit' =>  3,
                ),
            );
        }
}